package leetcode41_50;

import java.lang.reflect.Array;
import java.security.Key;
import java.util.*;

/**
 * @author shhjiang_001
 * @create 2020-06-18 12:26
 */
        /*
        给定一个字符串数组，将字母异位词组合在一起。字母异位词指字母相同，但排列不同的字符串。
        示例:
        输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
        输出:
        [
        ["ate","eat","tea"],
        ["nat","tan"],
        ["bat"]
        ]
        */

public class Solution49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> resList = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        int value = 0;
        for (int i = 0;i < strs.length;i++){
            char[] chars = strs[i].toCharArray();
            Arrays.sort(chars);
            String s = Arrays.toString(chars);
            if (!map.containsKey(s)){
                map.put(s,value);
                value++;
                ArrayList<String> strings = new ArrayList<>();
                strings.add(strs[i]);
                resList.add(strings);
            }else {
                Integer index = map.get(s);
                resList.get(index).add(strs[i]);
            }
        }
        return resList;
    }
}
